﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Dynamic;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using NetJsWire.Lite.Widgets.UI;
using Newtonsoft.Json.Serialization;

namespace NetJsWire.Lite.Resolver
{
    public class ComponentJsonResolver : DefaultContractResolver
    {
        public ComponentJsonResolver()
        {
            SerializeCompilerGeneratedMembers = true;
        }

        protected override JsonContract CreateContract( Type objectType )
        {
            if ( typeof( BaseComponent ).IsAssignableFrom( objectType ) )
            {
                var contract = CreateDynamicContract( objectType );
                //var contract = base.CreateContract( objectType );
                //MessageBox.Show( contract.GetType().Name + " is: " + ( typeof( IDynamicMetaObjectProvider ).IsAssignableFrom( objectType ) ) );
                //contract.OnSerializing = objectType.GetMethod( "OnSerializingMethod" );
                return contract;
            }
            
            return base.CreateContract( objectType );
        }

        //protected override List<MemberInfo> GetSerializableMembers( Type objectType )
        //{

        //    var members = TypeDescriptor.GetProperties( objectType ).Cast<PropertyDescriptor>();// base.GetSerializableMembers( objectType );
        //    var result = new List<MemberInfo>();
        //    foreach ( var member in members )
        //    {
        //        if ( member.Attributes.Contains( new IgnoreEmptyCollectionAttribute() ) )
        //        {
        //        }
        //        else
        //        {
        //        }
        //    }

        //    return result;
        //}

        protected override IList<JsonProperty> CreateProperties( Type type, Newtonsoft.Json.MemberSerialization memberSerialization )
        {
            if ( typeof( BaseComponent ).IsAssignableFrom( type ) )
                return base.CreateProperties( type, Newtonsoft.Json.MemberSerialization.OptIn );

            return base.CreateProperties( type, memberSerialization );
        }
    }
}
